﻿// CQueryRoomDlg.cpp: 实现文件
//

#include "pch.h"
#include "HotelManagementMFC.h"
#include "afxdialogex.h"
#include "CQueryRoomDlg.h"


// CQueryRoomDlg 对话框

IMPLEMENT_DYNAMIC(CQueryRoomDlg, CDialog)

CQueryRoomDlg::CQueryRoomDlg(CWnd* pParent /*=nullptr*/)
	: CDialog(IDD_DLG_ROOM_QUERY, pParent)
{

}

CQueryRoomDlg::~CQueryRoomDlg()
{
}

void CQueryRoomDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_LIST_ROOM, m_listRoom);
}


BEGIN_MESSAGE_MAP(CQueryRoomDlg, CDialog)
END_MESSAGE_MAP()


// CQueryRoomDlg 消息处理程序


BOOL CQueryRoomDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	// 设置动态窗口标题
	switch (theApp.roomFlag)
	{
	case theApp.ROOM_ALL:
		m_wTitle = "客房信息查询";
		break;
	case theApp.ROOM_EMPTY:
		m_wTitle = "空房信息查询";
		break;
	case theApp.ROOM_NOTFULL:
		m_wTitle = "未满房间信息查询";
		break;
	}
	SetWindowText(m_wTitle);

	// TODO:  在此添加额外的初始化
	m_listRoom.InsertColumn(0, L"房间编号", LVCFMT_CENTER, 80);
	m_listRoom.InsertColumn(1, L"房间类型", LVCFMT_CENTER, 80);
	m_listRoom.InsertColumn(2, L"额定人数", LVCFMT_CENTER, 80);
	m_listRoom.InsertColumn(3, L"实际人数", LVCFMT_CENTER, 80);
	m_listRoom.InsertColumn(4, L"是否有电视", LVCFMT_CENTER, 100);
	m_listRoom.InsertColumn(5, L"是否有空调", LVCFMT_CENTER, 100);
	m_listRoom.InsertColumn(6, L"是否有卫生间", LVCFMT_CENTER, 120);
	m_listRoom.InsertColumn(7, L"备注", LVCFMT_CENTER, 300);

	m_listRoom.ModifyStyle(0, LVS_REPORT | LVS_SHOWSELALWAYS);
	m_listRoom.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP);

	// 显示数据
	DisplayRoomInfo();

	return TRUE;  // return TRUE unless you set the focus to a control
	// 异常: OCX 属性页应返回 FALSE
}


void CQueryRoomDlg::DisplayRoomInfo()
{
	// TODO: 在此处添加实现代码.
	UpdateData(1);
	_variant_t Holder;
	CString strSQL, tmp;
	// 多条自增指针
	int i = 0;

	m_listRoom.DeleteAllItems();
	m_listRoom.SetRedraw(0);

	_RecordsetPtr pRs;
	pRs.CreateInstance(__uuidof(Recordset));

	switch (theApp.roomFlag)
	{
	case theApp.ROOM_ALL:
		strSQL = "Select RoomCode, RoomType, PeopleNum, FactPeopleNum, TV, AirCondition, Toilet, Remark From room";
		break;
	case theApp.ROOM_EMPTY:
		strSQL = "Select RoomCode, RoomType, PeopleNum, FactPeopleNum, TV, AirCondition, Toilet, Remark From room Where FactPeopleNum = 0";
		break;
	case theApp.ROOM_NOTFULL:
		strSQL = "Select RoomCode, RoomType, PeopleNum, FactPeopleNum, TV, AirCondition, Toilet, Remark From room Where FactPeopleNum < PeopleNum";
		break;
	}

	try {
		m_pRecordSet.CreateInstance(__uuidof(Recordset));
		m_pRecordSet->Open(strSQL.AllocSysString(),
			theApp.m_pConnection.GetInterfacePtr(),
			adOpenDynamic,
			adLockOptimistic,
			adCmdText);

		// 判断为空
		if (!m_pRecordSet->GetRecordCount() == 0)
		{
			m_pRecordSet->MoveFirst();
			while (!m_pRecordSet->adoEOF)
			{
				Holder = m_pRecordSet->GetCollect("RoomCode");
				if (Holder.vt != VT_NULL)
					m_listRoom.InsertItem(i, (_bstr_t)Holder);

				Holder = m_pRecordSet->GetCollect("RoomType");
				if (Holder.vt != VT_NULL)
				{
					tmp = (char*)(_bstr_t)Holder;
					CString str;
					str.Format(L"Select RoomTypeName From roomtype Where RoomTypeNo = '%s'", tmp);
					pRs->Open(str.AllocSysString(),
						theApp.m_pConnection.GetInterfacePtr(),
						adOpenDynamic,
						adLockOptimistic,
						adCmdText);
					if (pRs->RecordCount != 0)
					{
						Holder = pRs->GetCollect("RoomTypeName");
						m_listRoom.SetItemText(i, 1, (_bstr_t)Holder);
					}
					pRs->Close();
				}

				Holder = m_pRecordSet->GetCollect("PeopleNum");
				if (Holder.vt != VT_NULL)
					m_listRoom.SetItemText(i, 2, (_bstr_t)Holder);

				Holder = m_pRecordSet->GetCollect("FactPeopleNum");
				if (Holder.vt != VT_NULL)
					m_listRoom.SetItemText(i, 3, (_bstr_t)Holder);

				Holder = m_pRecordSet->GetCollect("TV");
				if (Holder.vt != VT_NULL)
				{
					tmp = (char*)(_bstr_t)Holder;
					if (tmp.CompareNoCase(L"1") == 0)
						m_listRoom.SetItemText(i, 4, L"有");
					else
						m_listRoom.SetItemText(i, 4, L"无");
				}

				Holder = m_pRecordSet->GetCollect("AirCondition");
				if (Holder.vt != VT_NULL)
				{
					tmp = (char*)(_bstr_t)Holder;
					if (tmp.CompareNoCase(L"1") == 0)
						m_listRoom.SetItemText(i, 5, L"有");
					else
						m_listRoom.SetItemText(i, 5, L"无");
				}

				Holder = m_pRecordSet->GetCollect("Toilet");
				if (Holder.vt != VT_NULL)
				{
					tmp = (char*)(_bstr_t)Holder;
					if (tmp.CompareNoCase(L"1") == 0)
						m_listRoom.SetItemText(i, 6, L"有");
					else
						m_listRoom.SetItemText(i, 6, L"无");
				}

				Holder = m_pRecordSet->GetCollect("Remark");
				if (Holder.vt != VT_NULL)
					m_listRoom.SetItemText(i, 7, (_bstr_t)Holder);

				m_pRecordSet->MoveNext();
				i++;
			}
		}
		else
			AfxMessageBox(L"没有房间数据！", MB_OK);
	}
	catch (_com_error& e)
	{
		AfxMessageBox(e.Description());
	}
	m_listRoom.SetRedraw(1);
}